package codename.carrot.bomberman.game;

import sheep.game.Game;
import sheep.game.Layer;
import sheep.math.BoundingBox;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import codename.carrot.bomberman.AppStorage;
import codename.carrot.bomberman.R;
import codename.carrot.bomberman.models.Level;

/**
 *  The GameLayer of DroidBomb, which the {@link Level} will be drawn on
 * @author Group A4.
 *
 */
public class GameLayer extends Layer {
	
	/**
	 * The level this gamelayer will be drawing
	 */
	private Level level;
	/**
	 * The background-bitmap (grass)
	 */
	private Bitmap tileBitmap;
	/**
	 * The paint-object to be used to draw {@link GameLayer#tileBitmap}
	 */
	private Paint tilePaint = new Paint();
	/**
	 *  The highest x-coordinate (based on the tile-system)
	 */
	private int xMax;
	/**
	 * The highest y-coordinate (based on the tile-system)
	 */
	private int yMax;
	
	/**
	 *  Constructor
	 * @param level the level to be played on
	 */
	public GameLayer(Level level) {
		this.level = level;		
		
		// Fetch the tile-bitmap for background
		tileBitmap = BitmapFactory.decodeResource(Game.getInstance()
				.getResources(), R.drawable.grass);
		xMax = level.getWidth() / AppStorage.TILE_SIZE;
		yMax = level.getHeight() / AppStorage.TILE_SIZE;			
	}		

	@Override
	public void update(float dt) {
		level.update(dt);
	}

	/**
	 * Returns the current level
	 * @return level the level this gamelayer draws
	 */
	public Level getLevel() {
		return level;
	}

	/**
	 *  Draws the level, as well as the background tiles (grass)
	 *  @param canvas the canvas to draw on	 
	 */
	@Override
	public void draw(Canvas canvas, BoundingBox box) {

		for (int xCoor = 0; xCoor < xMax; xCoor++) {
			for (int yCoor = 0; yCoor < yMax; yCoor++) {
				canvas.drawBitmap(tileBitmap, xCoor * AppStorage.TILE_SIZE, yCoor
						* AppStorage.TILE_SIZE, tilePaint);
			}
		}

		level.draw(canvas);
	}

}
